using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Fan.Dongping.DataBase;


namespace Fan.Dongping.Ali.DataAccess
{

    public class NoteDA
    {
        static NoteDA noteDa = null;
        public static NoteDA Instance
        {
            get
            {
                if (noteDa == null)
                {
                    noteDa = new NoteDA();
                }
                return noteDa;
            }
        }

        public DataTable GetNotes(string userId)
        {
            string sql = "select  * from Notes where UserId=@UserId";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@UserId", userId);
            DataSet ds = DA_Base.RunReturnDataset(sql, para);
            return ds.Tables[0];
        }

        public int AddNote(string userId, string topic, string noteBody)
        {
            string sql = "INSERT INTO Notes(UserId,Topic, Body, CreateTime) VALUES (@UserId,@Topic,@Body,GetDate())";

            SqlParameter[] paras = new SqlParameter[4];
            paras[0] = new SqlParameter("@UserId", userId);
            paras[1] = new SqlParameter("@Body", noteBody);
            paras[2] = new SqlParameter("@Topic", topic);

            return DA_Base.ExecuteNonQuery(sql, paras);
        }

        public int ModifyNote(long noteId, string topic, string noteBody)
        {
            string sql = "Update Notes set Topic = @Topic, Body = @Body, UpdateTime = GetDate() where NoteId = @NoteId";

            SqlParameter[] paras = new SqlParameter[4];
            paras[0] = new SqlParameter("@NoteId", noteId);
            paras[1] = new SqlParameter("@Body", noteBody);
            paras[2] = new SqlParameter("@Topic", topic);
            return DA_Base.ExecuteNonQuery(sql, paras);
        }

        public int DeleteNote(long noteId)
        {
            string sql = "delete Notes where NoteId = @NoteId";
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@NoteId", noteId);

            return DA_Base.ExecuteNonQuery(sql, paras);
        }

        public DataRow GetNote(long noteId)
        {
            string sql = "select * from Notes where NoteId = @NoteId";
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@NoteId", noteId);

            DataTable dt = DA_Base.RunReturnDataset(sql, paras).Tables[0];
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return dt.Rows[0];
            }
        }
    }
}
